<!-- Home.vue -->
<template>
  <div class="home">
    <!-- 用户信息区域 -->
    <div class="user-info animate__animated animate__bounce">{{ userInfo.realName }}({{ userInfo.deptInfo[0].deptName }})</div>

    <div class="menu">
      <div class="menu-group">
        <div class="menu-item" @click="scanOpen">
          <div class="menu-item-content">
            <div class="icon-wrapper">
              <i class="van-icon van-icon-scan"></i>
            </div>
            <span class="text">扫一扫</span>
          </div>
          <i class="van-icon van-icon-arrow"></i>
        </div>

        <router-link to="/dayRecords" class="menu-item">
          <div class="menu-item-content">
            <div class="icon-wrapper">
              <i class="van-icon van-icon-records"></i>
            </div>
            <span class="text">表单记录</span>
          </div>
          <i class="van-icon van-icon-arrow"></i>
        </router-link>

        <router-link to="/dayPlans" class="menu-item">
          <div class="menu-item-content">
            <div class="icon-wrapper">
              <i class="van-icon van-icon-clock-o"></i>
            </div>
            <span class="text">检查计划</span>
          </div>
          <i class="van-icon van-icon-arrow"></i>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { ref, onMounted, inject } from 'vue'
  import { useRouter } from 'vue-router'
  import { getUserInfo } from "@/api/common"
  import { QrStream } from "vue3-qr-reader"
  import * as dd from 'dingtalk-jsapi'

  const toast = inject("toast");
  const router = useRouter();
  const userName = ref('');
  const userInfo = ref(JSON.parse(localStorage.getItem("SAVE_USER_INFO")));

  const scanOpen = async () => {
    try {
      if(!isInDingTalk()) {
        toast.error("请用钉钉扫码")
        return
      }
      let qrResult
      qrResult = await new Promise((resolve, reject) => {
        dd.scan({
          type: 'qr',
          success: resolve,
          fail: (err) => {
            reject(new Error(err.errorMessage || '扫码失败'))
          }
        })
      })
      const queryString = qrResult.text.split('#')[1];
      router.push(queryString)

    } catch (error) {
      toast.msg(error.message)
    }
  }

  function isInDingTalk() {
    const userAgent = navigator.userAgent.toLowerCase();
    return /dingtalk/i.test(userAgent); // 不区分大小写匹配 "dingtalk"
  }

  onMounted(() => { })
</script>

<style scoped>
  .home {
    min-height: 100vh;
    background-color: #f7f8fa;
  }

  /* 用户信息区域样式 */
  .user-info {
    padding: 16px;
    font-size: 20px;
    font-weight: 600;
    color: #323233;
  }

  .menu {
    padding: 12px;
  }

  .menu-group {
    background: #fff;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  }

  .menu-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    text-decoration: none;
    color: #323233;
    border-bottom: 1px solid #ebedf0;
  }

  .menu-item:last-child {
    border-bottom: none;
  }

  .menu-item-content {
    display: flex;
    align-items: center;
    gap: 12px;
  }

  .icon-wrapper {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #1989fa;
  }

  .icon-wrapper i {
    font-size: 20px;
  }

  .text {
    font-size: 16px;
  }

  .van-icon-arrow {
    color: #969799;
    font-size: 16px;
  }
</style>